package com.zimmur.app.mocolink.manage.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zimmur.app.mocolink.domain.model.Banner;
import com.zimmur.app.mocolink.domain.util.DateUtils;
import com.zimmur.app.mocolink.manage.web.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.manage.web.common.StatusCodeEnum;
import com.zimmur.app.mocolink.service.IBannerService;
@Controller
@RequestMapping("/banner")
public class BannerController extends BaseController {
	@Autowired
	private IBannerService bannerService;
	@RequestMapping("/index")
	public String index(ModelMap model){
		List<Banner> banners = bannerService.selectAll();
		model.addAttribute("bannerList", banners);
		return "/center/banner/list";
	}
	@RequestMapping("/edit")
	public String edit(ModelMap model){
		Integer bannerId = getParam("bannerId", 0);
		Banner banner = bannerService.selectById(bannerId);
		model.addAttribute("banner", banner);
		return "/center/banner/edit";
	}
	@RequestMapping("/add")
	public String add(ModelMap model){
		Banner banner =new Banner();
		model.addAttribute("banner", banner);
		return "/center/banner/edit";
	}
	@ResponseBody
	@RequestMapping("/save")
	public ReturnJsonEntity save(Banner banner){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		int flag=0;
		if(banner.getId()!=null && banner.getId()>0){
			banner.setCreateTime(DateUtils.getNow());
			flag = bannerService.update(banner);
		}else{
			banner.setCreateTime(DateUtils.getNow());
			flag = bannerService.add(banner); 
		}
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/delete")
	public ReturnJsonEntity delete(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer bannerId = getParam("bannerId", 0);
		int flag = bannerService.delete(bannerId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
}
